package com.icss.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.icss.dao.MenuDao;
import com.icss.dao.ReviewBeanDao;
import com.icss.dao.reviewClassDao;
import com.icss.dao.reviewDao;
import com.icss.entity.Custom;
import com.icss.entity.Menu;
import com.icss.entity.Restaurant;
import com.icss.entity.Review;
import com.icss.entity.ReviewBean;
import com.icss.entity.ReviewClass;


@Controller
@RequestMapping("menu")
public class MenuController {
	@Autowired
	private MenuDao dao;
	@Autowired
	private ReviewBeanDao beandao;
	@Autowired
	private reviewClassDao rcdao;
	@Autowired
	private reviewDao rdao;
@RequestMapping("showmenuinfo")//显示菜品详情
public String fun1(ModelMap mp,Integer DishId,HttpServletRequest request){
	HttpSession session=request.getSession();
	session.setAttribute("DishId", DishId);
	List<Menu> list=dao.showmenuinfo(DishId);
	List<ReviewBean> beans=beandao.findByDishId(DishId);
	List<ReviewBean> reviewBeans=beandao.findByRCId(DishId);
	List<ReviewClass> rclist=rcdao.findAllClass();
	mp.addAttribute("reviewclass",rclist);
	mp.addAttribute("menuinfolist", list);
	mp.addAttribute("reviewbean", beans);
	mp.addAttribute("revicereview", reviewBeans);
	return "menujsp/MenuDetail";	
}

@RequestMapping("manmenu")//显示菜单管理界面
public String  fun2(ModelMap mp,HttpServletResponse response) throws IOException{
	List<Menu> list=dao.manmenu();
	mp.addAttribute("manmenulist",list);
	return "menujsp/MenuManage";
}
@RequestMapping("addmenu")//上传显示菜单
public String  fun3(Integer MenuClassid,String DishName,Integer DishPrice,String dishDescribe,@RequestParam("DishPicture")CommonsMultipartFile pic,HttpServletRequest request) throws Exception{	
	InputStream ins=pic.getInputStream();
	String path=request.getServletContext().getRealPath("upimg");
	String filename=DishName+System.currentTimeMillis()+".jpg";
	FileOutputStream fout=new FileOutputStream(path+"/"+filename);
	byte[] bt=new byte[100];
	while (ins.read(bt)>0) {
		fout.write(bt);	
	}
	fout.flush();
	fout.close();
	String DishPicture=filename;
	dao.addMenu(MenuClassid,DishName,DishPrice,DishPicture,dishDescribe);
	// 重定向到查询所有用户的Controller，测试图片回显
	return "redirect:manmenu.do";
}
@RequestMapping("searchmenu")//按种类和名称查询菜品
public String  fun4(Menu menu,ModelMap mp) throws IOException{
	List<Menu> list=dao.searchmenu(menu);
	mp.addAttribute("manmenulist",list);
	return "menujsp/MenuManage";
}
@RequestMapping("deletemenu")//按种类和名称查询菜品
public String  fun5(Integer DishId) throws IOException{
	int r=dao.deletemenu(DishId);
	return "redirect:manmenu.do";
}

@RequestMapping("updatemenu")//按种类和名称查询菜品
public String  fun6(Integer MenuClassid,String DishName,Integer DishPrice,String dishDescribe,@RequestParam("DishPicture")CommonsMultipartFile pic,HttpServletRequest request,String DishState,Integer DishId) throws IOException{
	InputStream ins=pic.getInputStream();
	String path=request.getServletContext().getRealPath("upimg");
	String filename=DishName+System.currentTimeMillis()+".jpg";
	FileOutputStream fout=new FileOutputStream(path+"/"+filename);
	byte[] bt=new byte[100];
	while (ins.read(bt)>0) {
		fout.write(bt);	
	}
	fout.flush();
	fout.close();
	String DishPicture=filename;
	int r=dao.updatemenu(MenuClassid,DishName,DishPrice,DishPicture,DishState,DishId,dishDescribe);
	return "redirect:manmenu.do";
}
@RequestMapping("showmenu")//显示菜品详情
public String fun7(ModelMap mp,Integer DishId){
	List<Menu> list=dao.showmenuinfo(DishId);
	mp.addAttribute("menulist", list);
	return "menujsp/UpdateMenu";	
}
@RequestMapping("recommand")
public String fun8(ModelMap mp){
	List<Menu> list=dao.recommand();
	List<Menu> mList=dao.manmenu();
	List<Menu> eList=dao.eatmenu();
	List<Menu> dList=dao.drinkmenu();
	List<Restaurant> rList=dao.showrestaurant();
	mp.addAttribute("hlist", list);
	mp.addAttribute("mlist", mList);
	mp.addAttribute("elist", eList);
	mp.addAttribute("dlist", dList);
	mp.addAttribute("rlist", rList);
	return "index";	
}
@RequestMapping("menukind")
public String fun9(Integer MenuClassid,ModelMap mp){
	List<Menu> list=dao.menukind(MenuClassid);
	mp.addAttribute("mklist", list);
	return "menujsp/MenuKind";
}
@RequestMapping("manrestaurant")
public String fun10(ModelMap mp){
	List<Restaurant> list=dao.showrestaurant();
	mp.addAttribute("rlist", list);
	return "menujsp/ManRestaurant";
}
@RequestMapping("showrestaurant")
public String fun11(ModelMap mp){
	List<Restaurant> list=dao.showrestaurant();
	mp.addAttribute("rlist", list);
	
	return "menujsp/UpdateRestaurant";
}
@RequestMapping("updaterestaurant")
public String fun12(ModelMap mp,String RName,String RAddress,int RTel,int RId){
	int r=dao.updaterestaurant(RName,RAddress,RTel,RId);
	return "redirect:manrestaurant.do";
}
@RequestMapping("deletewish")
public String fun14(Integer id){
	int r=dao.deletewish(id);
	return "redirect:selectwish.do";
}

@RequestMapping("selectwish")
public String fun13(ModelMap mp,HttpServletRequest request){
	HttpSession session=request.getSession();
	Custom custom=(Custom) session.getAttribute("custom");
	int c_id=custom.getC_id();
	System.out.println(c_id);
	List<Menu> list=dao.selectwish(c_id);
	mp.addAttribute("collect", list);
	return "MyCollect";
}
@RequestMapping("addtowish")
@ResponseBody
public int fun15(Integer DishId, HttpServletRequest request,Integer c_id){
	System.out.println(c_id);
	int r=dao.inserwish(DishId,c_id);
	if(r>0) {
		
		return 1;
	}
	else {
		return 0;
	}
}
@RequestMapping("reviewIns") 
public String insReview(Review review,HttpServletRequest request) {
	HttpSession session=request.getSession();
	Custom custom=(Custom) session.getAttribute("custom");
	Integer dishId=(Integer) session.getAttribute("DishId");
 	Integer c_id=custom.getC_id();
	review.setC_id(c_id);
	review.setDishId(dishId);
	System.out.println("添加");
	int r=rdao.insReview(review);
	if (r>0) {
		String url="redirect:showmenuinfo.do?DishId="+dishId;
		System.out.println(dishId);
		return url;
	}
	else {
		return "menujsp/MenuDetail";
	}
}
@RequestMapping("reviewReply") 
public String reviewReply(Review review,HttpServletRequest request) {
	HttpSession session=request.getSession();
	Custom custom=(Custom) session.getAttribute("custom");
	Integer dishId=(Integer) session.getAttribute("DishId");
    Integer c_id=custom.getC_id();
	review.setC_id(c_id);
	review.setDishId(dishId);
	System.out.println("回复");
	int r=rdao.repReview(review);
	if (r>0) {
		String url="redirect:showmenuinfo.do?DishId="+dishId;
		System.out.println(dishId);
		return url;
	}
	else {
		return "menujsp/MenuDetail";
	}
}
}
